﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DrawZoneCom;
using YArchitech.Controls;
using YArchitech.Revit;
using HYCodeBase.WinForm;

namespace SelectTypeClass
{
	public class CurveloopsSelector
	{
		public static List<CurveLoop> GetUserSelectedLoops(ExternalCommandData commandData, Autodesk.Revit.DB.Document docAr)
		{
			List<CurveLoop> list = new List<CurveLoop>();
			FormSelectType formSelectType = new FormSelectType();
			if (DialogResult.OK == formSelectType.ShowDialog())
			{
				switch (formSelectType.GetSelectType())
				{
				case 0:
				{
					ViewPlan viewPlan = commandData.Application.ActiveUIDocument.ActiveView as ViewPlan;
					if (viewPlan == null)
					{
						return list;
					}
					CurveLoop viewCurveLoop = CurveloopsSelector.GetViewCurveLoop(viewPlan);
					if (viewCurveLoop != null)
					{
						list.Add(viewCurveLoop);
						return list;
					}
					return list;
				}
				case 1:
					using (List<List<CurveLoop>>.Enumerator enumerator = CurveloopsSelector.GetSelectFireArea(commandData.Application.ActiveUIDocument, docAr).GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							List<CurveLoop> list2 = enumerator.Current;
							if (list2.Count > 0)
							{
								list.Add(list2.First<CurveLoop>());
							}
						}
						return list;
					}
					break;
				case 2:
					break;
				default:
					return list;
				}
                foreach (List<CurveLoop> list3 in DrawZoneCom.DrawZoneCom.DrawZoneManual(commandData.Application, new HYCodeBase.WinForm.WindowHandle(Process.GetCurrentProcess().MainWindowHandle)))
				{
					if (list3.Count > 0)
					{
						list.Add(list3.First<CurveLoop>());
					}
				}
			}
			return list;
		}

		public static CurveLoop GetViewCurveLoop(ViewPlan viewPlan)
		{
			CurveLoop curveLoop = new CurveLoop();
			Transaction transaction = new Transaction(viewPlan.Document);
			transaction.Start("CropBox");
			try
			{
				bool cropBoxActive = viewPlan.CropBoxActive;
				viewPlan.CropBoxActive = !cropBoxActive;
				Level genLevel = viewPlan.GenLevel;
				if (genLevel == null)
				{
					return curveLoop;
				}
				double projectElevation = genLevel.ProjectElevation;
				BoundingBoxXYZ cropBox = viewPlan.CropBox;
				XYZ min = cropBox.Min;
				XYZ max = cropBox.Max;
				XYZ xyz = new XYZ(min.X, min.Y, projectElevation);
				XYZ xyz2 = new XYZ(min.X, max.Y, projectElevation);
				XYZ xyz3 = new XYZ(max.X, max.Y, projectElevation);
				XYZ xyz4 = new XYZ(max.X, min.Y, projectElevation);
				curveLoop.Append(Line.CreateBound(xyz, xyz2));
				curveLoop.Append(Line.CreateBound(xyz2, xyz3));
				curveLoop.Append(Line.CreateBound(xyz3, xyz4));
				curveLoop.Append(Line.CreateBound(xyz4, xyz));
				viewPlan.CropBoxActive = cropBoxActive;
				transaction.RollBack();
			}
			catch
			{
				transaction.RollBack();
			}
			return curveLoop;
		}

		public static List<List<CurveLoop>> GetSelectFireArea(UIDocument uiDoc, Autodesk.Revit.DB.Document docAr)
		{
			List<List<CurveLoop>> list = new List<List<CurveLoop>>();
			List<Area> list2 = new List<Area>();
			list2 = CurveloopsSelector.GetFireArea(uiDoc, docAr);
			if (list2.Count < 1)
			{
				YJKMessageBox.Show("没有选择到相应的防火分区！");
				return list;
			}
			foreach (Area area in list2)
			{
				List<CurveLoop> item = new List<CurveLoop>();
				item = CurveloopsSelector.GetZoneBoundaryCurves(area);
				list.Add(item);
			}
			return list;
		}

		private static List<Area> GetFireArea(UIDocument uiDoc, Autodesk.Revit.DB.Document docAr)
		{
			List<Area> list = new List<Area>();
			try
			{
				foreach (Reference reference in uiDoc.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)5, new SpatialElementSelFilter(true)))
				{
					Area area = docAr.GetElement(reference.LinkedElementId) as Area;
					if (area != null)
					{
						ElementId elementId = area.get_Parameter(BuiltInParameter.AREA_SCHEME_ID).AsElementId();
						if ((docAr.GetElement(elementId) as AreaScheme).Name.Contains("防火分区"))
						{
							list.Add(area);
						}
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public static List<CurveLoop> GetZoneBoundaryCurves(Area area)
		{
			List<CurveLoop> list = new List<CurveLoop>();
			bool flag = true;
			foreach (IList<BoundarySegment> list2 in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
			{
				CurveLoop curveLoop = new CurveLoop();
				if (flag)
				{
					foreach (BoundarySegment segment in list2)
					{
						curveLoop.Append(segment.GetCurve());
					}
					flag = false;
				}
				else
				{
					foreach (BoundarySegment segment2 in list2)
					{
						curveLoop.Append(segment2.GetCurve());
					}
				}
				list.Add(curveLoop);
			}
			return list;
		}
	}
}
